import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from "../utils/axios";

const titleGeneration = () => import('../views/writing/titleGeneration/titleGeneration');
const BackgroundHostingCreate = () => import('../views/writing/titleGeneration/BackgroundHostingCreate');
const BackgroundHostingList = () => import('../views/writing/titleGeneration/BackgroundHostingList');
const myArticles = () => import('../views/writing/myArticles/myArticles');
const planTorelease = () => import('../views/writing/planTorelease/planTorelease');
const siteManagement = () => import('../views/writing/siteManagement/siteManagement');
const sitEprogram = () => import('../views/writing/siteManagement/sitEprogram');
const parameterSettings = () => import('../views/writing/siteManagement/parameterSettings');
const autoEprogram = () => import('../views/writing/siteManagement/autoEprogram');
const autoSetting = () => import('../views/writing/siteManagement/autoSetting');
const websiteInformation = () => import('../views/writing/siteManagement/websiteInformation');
const addedSuccessfully = () => import('../views/writing/siteManagement/addedSuccessfully');
const InterfaceTest = () => import('../views/writing/siteManagement/InterfaceTest');
const releaselog = () => import('../views/writing/releaselog/releaselog');
const pictureManagement = () => import('../views/writing/pictureManagement/pictureManagement');
const ProhibitedWords = () => import('../views/writing/ProhibitedWords/ProhibitedWords');
const login = () => import("@c/common/tabbar/login")
const integral = () => import('../views/integral/pointsDetails/pointsDetails');
const rechargeDetails = () => import('../views/integral/rechargeDetails/rechargeDetails');
const rechargeChatDetails = () => import('../views/integral/rechargeChatDetails/rechargeChatDetails');
const basicInformation = () => import('../views/memberCentre/basicInformation/basicInformation');
const loginLog = () => import('../views/memberCentre/loginLog/loginLog');
const signInWith = () => import('../views/memberCentre/signInWith/signInWith');
const emailFirstStep = () => import('../views/memberCentre/basicInformation/emailFirstStep');
const emailStepTwo = () => import('../views/memberCentre/basicInformation/emailStepTwo');
const EmailStepThree = () => import('../views/memberCentre/basicInformation/EmailStepThree');
const mobileFirstStep = () => import('../views/memberCentre/basicInformation/mobileFirstStep');
const mobileStepTwo = () => import('../views/memberCentre/basicInformation/mobileStepTwo');
const mobileStepThree = () => import('../views/memberCentre/basicInformation/mobileStepThree');
const copyWriting = () => import('../views/copyWriting/platformCopywriting/platformCopywriting');
const chatDoghome = () => import('../views/chatDog/home/home');
const myPromotion = () => import('../views/toPromote/myPromotion/myPromotion');
const myClient = () => import('../views/toPromote/myClient/myClient');
const myIncome = () => import('../views/toPromote/myIncome/myIncome');
const withdrawalsRecord = () => import('../views/toPromote/withdrawalsRecord/withdrawalsRecord');
const manageBots = () => import('../views/alRobot/manageBots/manageBots');
const myRobotDataAnalysis = () => import('../views/alRobot/myRobotDataAnalysis/myRobotDataAnalysis');
const myRobotChat = () => import('../views/alRobot/myRobotChat/myRobotChat');
const myRobotUserDialog = () => import('../views/alRobot/myRobotUserDialog/myRobotUserDialog');
const myRobotUserKnowledge = () => import('../views/alRobot/myRobotUserKnowledge/myRobotUserKnowledge');
const addKnowledgeBase = () => import('../views/alRobot/myRobotUserKnowledge/addKnowledgeBase');
const myRobotUserInstall = () => import('../views/alRobot/myRobotUserInstall/myRobotUserInstall');
const myRobotAPI = () => import('../views/alRobot/myRobotAPI/myRobotAPI');
const myRobotSetUp = () => import('../views/alRobot/myRobotSetUp/myRobotSetUp');
const NewAiRobot = () => import('../views/alRobot/NewAiRobot/NewAiRobot');
const NewAiRobotDetails = () => import('../views/alRobot/NewAiRobotDetails/NewAiRobotDetails');
const NewAiRobotRenew = () => import('../views/alRobot/NewAiRobotRenew/NewAiRobotRenew');
const NewAiRobotNewPurchase = () => import('../views/alRobot/NewAiRobotNewPurchase/NewAiRobotNewPurchase');
const NewAiRobotSuccessful = () => import('../views/alRobot/NewAiRobotSuccessful/NewAiRobotSuccessful');

Vue.use(VueRouter);

const routes = [
        {
            path: '',
            redirect: '/writing/titleGeneration/titleGeneration',
            component: titleGeneration,
            meta: {
                auth: false,
                title: '文章生成-AiDOG'
            }
        },
        {
            path: '/writing',
            component: titleGeneration,
            meta: {
                auth: false,
                title: '文章生成-AiDOG'
            }
        },
        {
            path: '/alRobot',
            component: NewAiRobot,
            meta: {
                auth: false,
                title: '管理机器人'
            }
        },
        {
            path: '/alRobot/NewAiRobotSuccessful/NewAiRobotSuccessful',
            component: NewAiRobotSuccessful,
            meta: {
                auth: false,
                addClass2: true,
                title: '支付成功'
            }
        },
        {
            path: '/alRobot/NewAiRobotNewPurchase/NewAiRobotNewPurchase',
            component: NewAiRobotNewPurchase,
            meta: {
                auth: false,
                addClass2: true,
                title: '新购'
            }
        },
        {
            path: '/alRobot/NewAiRobotRenew/NewAiRobotRenew',
            component: NewAiRobotRenew,
            meta: {
                auth: false,
                addClass2: true,
                title: '续费'
            }
        },
        {
            path: '/alRobot/NewAiRobotDetails/NewAiRobotDetails',
            component: NewAiRobotDetails,
            meta: {
                auth: false,
                addClass2: true,
                title: '升级订单确认'
            }
        },
        {
            path: '/alRobot/NewAiRobot/NewAiRobot',
            component: NewAiRobot,
            meta: {
                auth: false,
                title: '管理机器人'
            }
        },
        {
            path: '/alRobot/myRobotSetUp/myRobotSetUp',
            component: myRobotSetUp,
            meta: {
                auth: false,
                title: '我的机器人-设置'
            }
        },
        {
            path: '/alRobot/myRobotAPI/myRobotAPI',
            component: myRobotAPI,
            meta: {
                auth: false,
                title: '我的机器人-API'
            }
        },
        {
            path: '/alRobot/myRobotUserInstall/myRobotUserInstall',
            component: myRobotUserInstall,
            meta: {
                auth: false,
                title: '我的机器人-安装'
            }
        },
        {
            path: '/alRobot/myRobotUserKnowledge/addKnowledgeBase',
            component: addKnowledgeBase,
            meta: {
                auth: true,
                addKnow: true,
                title: '我的机器人-知识库-添加知识库'
            }
        },
        {
            path: '/alRobot/myRobotUserKnowledge/myRobotUserKnowledge',
            component: myRobotUserKnowledge,
            meta: {
                auth: false,
                title: '我的机器人-知识库'
            }
        },
        {
            path: '/alRobot/myRobotUserDialog/myRobotUserDialog',
            component: myRobotUserDialog,
            meta: {
                auth: false,
                title: '我的机器人-用户对话'
            }
        },
        {
            path: '/alRobot/myRobotChat/myRobotChat',
            component: myRobotChat,
            meta: {
                auth: false,
                title: '我的机器人-聊天'
            }
        },
        {
            path: '/alRobot/myRobotDataAnalysis/myRobotDataAnalysis',
            component: myRobotDataAnalysis,
            meta: {
                auth: false,
                title: '我的机器人-数据分析'
            }
        },
        {
            path: '/copyWriting',
            component: copyWriting,
            meta: {
                auth: true,
                title: '文案创作-AiDOG'
            }
        },
        {
            path: '/chatDog',
            component: chatDoghome,
            meta: {
                auth: true,
                title: 'ChatDOD-AiDOG'
            }
        },
        // {
        //     path: '/toPromote',
        //     component: myPromotion,
        //     meta: {
        //         auth: true,
        //         toPromote: true,
        //         title: '我要推广-AiDOG'
        //     }
        // },
        {
            path: '/toPromote/withdrawalsRecord',
            component: withdrawalsRecord,
            meta: {
                auth: true,
                toPromote: true,
                title: '提现记录-AiDOG'
            }
        },
        {
            path: '/toPromote/myIncome',
            component: myIncome,
            meta: {
                auth: true,
                toPromote: true,
                title: '我的收益-AiDOG'
            }
        },
        {
            path: '/writing/titleGeneration/BackgroundHostingList',
            component: BackgroundHostingList,
            meta: {
                auth: true,
                requiresAuth: true,
                requiresA: true,
                requiresC: true,
                title: '文章生成-AiDOG'
            }
        },
        {
            path: '/writing/titleGeneration/BackgroundHostingCreate',
            component: BackgroundHostingCreate,
            meta: {
                auth: true,
                requiresAuth: true,
                requiresA: true,
                requiresC: true,
                title: '文章生成-AiDOG'
            }
        },
        {
            path: '/toPromote/myClient',
            component: myClient,
            meta: {
                auth: true,
                toPromote: true,
                title: '我的客户-AiDOG'
            }
        },
        {
            path: '/toPromote/myPromotion',
            component: myPromotion,
            meta: {
                auth: true,
                toPromote: true,
                title: '我的推广-AiDOG'
            }
        },
        // {
        //     path: '/chatDog/home/home',
        //     component: chatDoghome,
        //     meta: {
        //         auth: true,
        //         title: 'ChatDOG-AiDOG'
        //     }
        // },
        {
            path: '/copyWriting/platformCopywriting/platformCopywriting',
            component: copyWriting,
            meta: {
                auth: true,
                title: '文案创作-AiDOG'
            }
        },
        {
            path: '/writing/titleGeneration/titleGeneration',
            component: titleGeneration,
            meta: {
                auth: false,
                requiresAuth: true,
                requiresA: true,
                title: '文章生成-AiDOG'
            }
        },
        {
            path: '/writing/myArticles',
            component: myArticles,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '我的文章-AiDOG'
            }
        },
        {
            path: '/writing/planTorelease',
            component: planTorelease,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '计划发布-AiDOG'
            }
        },
        {
            path: '/writing/siteManagement',
            component: siteManagement,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '站点管理-AiDOG'
            }
        },
        {
            path: '/writing/siteManagement/sitEprogram',
            component: sitEprogram,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '站点管理-AiDOG'
            }
        },
        {
            path: '/writing/siteManagement/autoEprogram',
            component: autoEprogram,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '站点管理-站点匹配'
            }
        },
        {
            path: '/writing/siteManagement/autoSetting',
            component: autoSetting,
			name:'autoSetting',
            meta: {
                auth: true,
                requiresAuth: true,
                title: '站点管理-安装插件'
            }
        },
        {
            path: '/writing/siteManagement/parameterSettings',
            component: parameterSettings,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '站点管理-AiDOG'
            }
        },
        {
            path: '/writing/siteManagement/websiteInformation',
            component: websiteInformation,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '站点管理-AiDOG'
            }
        },
        {
            path: '/writing/siteManagement/addedSuccessfully',
            component: addedSuccessfully,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '站点管理-AiDOG'
            }
        },
        {
            path: '/writing/siteManagement/InterfaceTest',
            component: InterfaceTest,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '站点管理-AiDOG'
            }
        },
        {
            path: '/writing/releaselog',
            component: releaselog,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '发布日志-AiDOG'
            }
        },
        {
            path: '/writing/pictureManagement',
            component: pictureManagement,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '图片管理-AiDOG'
            }
        },
        {
            path: '/writing/ProhibitedWords',
            component: ProhibitedWords,
            meta: {
                auth: true,
                requiresAuth: true,
                title: '违禁词管理-AiDOG'
            }
        },
        {
            path: '/login',
            component: login,
            meta: {
                auth: false,
                title: '登录-AiDOG'
            }
        },
        {
            path: '/integral',
            component: integral,
            meta: {
                auth: true,
                integral: true,
                title: '积分明细-AiDOG'
            }
        },
        {
            path: '/integral/pointsDetails',
            component: integral,
            meta: {
                auth: true,
                integral: true,
                title: '积分明细-AiDOG'
            }
        },
        {
            path: '/integral/rechargeDetails',
            component: rechargeDetails,
            meta: {
                auth: true,
                integral: true,
                title: '发票申请-AiDOG'
            }
        },
        {
            path: '/integral/rechargeChatDetails',
            component: rechargeChatDetails,
            meta: {
                auth: true,
                integral: true,
                title: '订单明细-AiDOG'
            }
        },
        {
            path: '/memberCentre',
            component: basicInformation,
            meta: {
                auth: true,
                memberCentre: true,
                title: '会员-AiDOG',
            }
        },
        {
            path: '/memberCentre/basicInformation',
            component: basicInformation,
            meta: {
                auth: true,
                memberCentre: true,
                title: '会员基本信息-AiDOG'
            }
        },
        {
            path: '/memberCentre/loginLog',
            component: loginLog,
            meta: {
                auth: true,
                memberCentre: true,
                title: '登录日志-AiDOG'
            }
        },
        {
            path: '/memberCentre/signInWith',
            component: signInWith,
            meta: {
                auth: true,
                memberCentre: true,
                title: '会员-AiDOG'
            }
        },
        {
            path: '/memberCentre/basicInformation/emailFirstStep',
            component: emailFirstStep,
            meta: {
                auth: true,
                memberCentre: true,
                title: '会员-AiDOG'
            }
        },
        {
            path: '/memberCentre/basicInformation/emailStepTwo',
            component: emailStepTwo,
            meta: {
                auth: true,
                memberCentre: true,
                title: '会员-AiDOG'
            }
        },
        {
            path: '/memberCentre/basicInformation/EmailStepThree',
            component: EmailStepThree,
            meta: {
                auth: true,
                memberCentre: true,
                title: '会员-AiDOG'
            }
        },
        {
            path: '/memberCentre/basicInformation/mobileFirstStep',
            component: mobileFirstStep,
            meta: {
                auth: true,
                memberCentre: true,
                title: '会员-AiDOG'
            }
        },
        {
            path: '/memberCentre/basicInformation/mobileStepTwo',
            component: mobileStepTwo,
            meta: {
                auth: true,
                memberCentre: true,
                title: '会员-AiDOG'
            }
        },
        {
            path: '/memberCentre/basicInformation/mobileStepThree',
            component: mobileStepThree,
            meta: {
                auth: true,
                memberCentre: true,
                title: '会员-AiDOG'
            }
        },

];

const router = new VueRouter({
    // mode: 'history',
    base: process.env.BASE_URL,
    routes
});

// 路由拦截器
router.beforeEach(async (to, from, next) => {
    let promotion_id = to.query.ref
    if(promotion_id){
        localStorage.setItem('promotion_id', promotion_id.toString())
    }
	
	// 触发百度统计页面浏览事件
	if (window._hmt) {
		window._hmt.push(['_trackPageview', to.fullPath])
	}
	
	
	
    redirectToMiniProgram();
    if(to.meta.title){
        document.title = to.meta.title
    }else{
		document.title = 'AIDOG'; // 如果没有定义meta标题，则使用默认标题
	}
    if (to.matched.some(record => record.meta.auth) && to.meta.auth) { // 判断该路由是否需要登录权限
        let token = localStorage.getItem('token');
        if (token) { // 获取当前的 token 是否存在
            next()
        } else {
            // 不存在 token，需要重新认证
            next({
                path: '/login',
                query: {
                    back_url: encodeURIComponent(to.fullPath)
                }
            })
        }
    }
    next();
});


// 判断是否在移动设备上
function isMobileDevice() {
    return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}

// 跳转到微信小程序
function redirectToMiniProgram() {
    // 在移动设备上，使用微信小程序的跳转逻辑
    if (isMobileDevice()) {
        let promotion_id = localStorage.getItem('promotion_id')
        let data = {};
        if(promotion_id){
            data.promotion_id = promotion_id
        }
        // 通过修改以下参数来指定要跳转的微信小程序
        axios.post('/api/common/getUrlScheme',data).then(
            res => {
                location.href = res.result.openlink
            }
        )
    } else {
        // console.log('非移动设备，无需跳转到微信小程序');
    }
}

// 调用跳转函数




export default router
